package com.yykj.tencent.im.server.service.impl;

import com.yykj.tencent.im.server.bean.callback.FriendAddCallback;
import com.yykj.tencent.im.server.bean.callback.FriendDeleteCallback;
import com.yykj.tencent.im.server.dto.ContactDTO;
import com.yykj.tencent.im.server.dto.FriendDTO;
import com.yykj.tencent.im.server.dto.GroupDTO;
import com.yykj.tencent.im.server.exception.TimException;
import com.yykj.tencent.im.server.model.entity.Friend;
import com.yykj.tencent.im.server.model.repository.FriendRepository;
import com.yykj.tencent.im.server.service.FriendService;
import com.yykj.tencent.im.server.utils.Map2Object;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FriendServiceImpl implements FriendService {

    @Value("${environment}")
    private String environment;

    private FriendRepository friendRepository;

    @Autowired
    public void setFriendRepository(FriendRepository friendRepository){
        this.friendRepository = friendRepository;
    }

    /**
     * 添加好友之后回调
     */
    @Override
    @Async
    public void addFriendCallback(Map<String, Object> map) {
        // map->FriendAddCallback
        FriendAddCallback fac = (FriendAddCallback) Map2Object.mapToObject(map,FriendAddCallback.class);
        List<Friend> lstFriend = new ArrayList<>();
        List<FriendAddCallback.PairItem> pair = fac.getPairList();
        for(FriendAddCallback.PairItem pi:pair){
            Friend friend = new Friend();
            friend.setFromAccount(pi.getFromAccount());
            friend.setToAccount(pi.getToAccount());
            friend.setEnvironment(environment);
            lstFriend.add(friend);
        }
        friendRepository.saveAll(lstFriend);
    }

    /**
     * 删除好友之后回调
     */
    @Override
    @Async
    public void deleteFriendCallback(Map<String, Object> map) {
        FriendDeleteCallback fac = (FriendDeleteCallback) Map2Object.mapToObject(map,FriendDeleteCallback.class);
        List<FriendDeleteCallback.PairItem> pair = fac.getPairList();
        for(FriendDeleteCallback.PairItem pi:pair){
            friendRepository.deleteMyFriend(environment,pi.getFromAccount(),pi.getToAccount());
        }
    }

    /**
     * 常用联系好友/团队
     */
    @SneakyThrows
    @Override
    public List<ContactDTO> contact(String fromAccount) {
        if(fromAccount == null || StringUtils.isEmpty(fromAccount)){
            throw new TimException("账号ID不能为空");
        }
        Sort sort = Sort.by(Sort.Order.desc("number"));
        Pageable pageable = PageRequest.of(0, 10,sort);
        Page<Friend> friends = friendRepository.queryFriend(environment,fromAccount,pageable);
        List<Friend> list = friends.getContent();
        //常用联系好友ID
        List<String> accounts = new ArrayList<>();
        //常用联系团队ID
        List<String> groupIds = new ArrayList<>();
        for(Friend f:list){
            if(f.getType().equals("F")){
                accounts.add(f.getToAccount());
            }else{
                groupIds.add(f.getToAccount());
            }
        }
        List<ContactDTO> contactDTOS = new ArrayList<>();
        //常用联系团队
        if(groupIds.size() > 0){
            List<GroupDTO> groupDTOS = friendRepository.queryGroupById(groupIds);
            for(GroupDTO groupDTO:groupDTOS){
                contactDTOS.add(new ContactDTO(groupDTO.getGroupId(),groupDTO.getFaceUrl(),groupDTO.getName()));
            }
        }

        //常用联系好友
        if(accounts.size() > 0){
            List<FriendDTO> friendDTOS = friendRepository.queryFriendByAccount(accounts);
            for(FriendDTO friendDTO:friendDTOS){
                contactDTOS.add(new ContactDTO(friendDTO.getToAccount(),friendDTO.getImage(),friendDTO.getName()));
            }
        }
        return contactDTOS;
    }


    /**
     * 我的好友
     */
    @SneakyThrows
    @Override
    public List<ContactDTO> myFriends(String fromAccount) {
        if(fromAccount == null || StringUtils.isEmpty(fromAccount)){
            throw new TimException("账号ID不能为空");
        }
        List<Friend> friends = friendRepository.myFriends(environment,fromAccount,"F");
        //好友ID
        List<String> accounts = new ArrayList<>();
        for(Friend friend:friends){
            accounts.add(friend.getToAccount());
        }
        List<ContactDTO> contactDTOS = new ArrayList<>();
        if(accounts.size() > 0){
            List<FriendDTO> friendDTOS = friendRepository.queryFriendByAccount(accounts);
            for(FriendDTO friendDTO:friendDTOS){
                contactDTOS.add(new ContactDTO(friendDTO.getToAccount(),friendDTO.getImage(),friendDTO.getName()));
            }
        }
        return contactDTOS;
    }






}
